package com.example.datn.utils;




import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.util.Log;

import com.example.datn.objects.RssObject;


public class RssParser {

	 
	
	
	
	
	
	public List<RssObject> getNewList(String link) {
        try {
            // URL url= new URL(link);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(link);
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setValidating(false);
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            RssHandler handler = new RssHandler();
            reader.setContentHandler(handler);
            // InputSource source= new InputSource(url.openStream());
            // reader.parse(source);
            InputSource inStream = new InputSource();
            inStream.setCharacterStream(new StringReader(EntityUtils
                    .toString(entity)));
            reader.parse(inStream);
            return handler.getItemList();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            Log.d("RSS:","Cannot connect RSS!");
            return null;
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
            Log.d("RSS:","Cannot connect RSS!");
            return null;
        } catch (SAXException e) {
            e.printStackTrace();
            Log.d("RSS:","Cannot connect RSS!");
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("RSS:","Cannot connect RSS!");
            return null;
        }
    }
 
}